package lormi.meipin.common;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Result {

	private static final ObjectMapper mapper = new ObjectMapper();
	
	private int result;// 1表示成功 0表示失败
	private String code;// 具体的编号，自定义
	private String message;// 对应编号的异常信息
	private Object object;// 数据 如果没有就是空

	public Result() {
	}

	public Result(int result, String code, String message) {
		this.result = result;
		this.code = code;
		this.message = message;
	}

	public Result(int result, String code, String message, Object object) {
		this.result = result;
		this.code = code;
		this.message = message;
		this.object = object;
	}
	
	public static String success() {
		return new Result(1, "100000", "成功").toString();
	}
	
	public static String success(Object obj){
		return new Result(1, "100000", "成功", obj).toString();
	}

	@Override
	public String toString() {
		try {
			return "{\"result\":\"" + this.result + "\",\"code\":\"" + this.code + "\",\"msg\":\"" + this.message
					+ "\",\"data\":" + mapper.writeValueAsString(this.object) + "}";
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Object getObject() {
		return object;
	}

	public void setObject(Object object) {
		this.object = object;
	}
}
